Geospatial Maps with R

Map Data

# open source map data library
library(cancensus)

# spatial data frame, specify geography
census_data <- get_census(dataset='CA16', regions=list(C="1"),
                          vectors=c("median_hh_income"="v_CA16_2397"),
                          level='CD', use_cache = FALSE, geo_format = 'sf')
#what does this data look like?
summary(census_data$median_hh_income)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   34944   54833   63147   65971   72662  192979

Base Map

library(leaflet) #call map making library

#set map projection
m<-leaflet(census_data) %>% 
  addProviderTiles(providers$CartoDB) %>%
  setView(-99, 56, 3.5) #set longitude, latitude, and zoom

Base Map

Prepare Colors and Measures

library(viridis) #for pretty colours

#set income level thresholds
bins <- c(0,50000,65000,75000,90000,110000, Inf)

#set colour palette
pal <- colorBin("viridis", domain = census_data$v_CA16_2397, bins = bins)

#polygons from dataframe with colours
m %>% addPolygons(
  fillColor = ~pal(median_hh_income),
  color = "white",
  weight = 0.5,
  opacity = 1,
  fillOpacity = 0.65)

Polygons with Colour

Add Interactivity

m %>% addPolygons(fillColor = ~pal(median_hh_income),
              color = "white",
              weight = 0.5,
              opacity = 1,
              fillOpacity = 0.65,
              highlightOptions = highlightOptions(
                weight = 3,
                fillOpacity = 0.8,
                color = "white",
                bringToFront = TRUE))

Add Interactivity

Add Bold Data Labels

library(htmltools) #for inserting labels

#make the labels
labels <- sprintf("<strong>$%s", census_data$median_hh_income) %>% 
  lapply(htmltools::HTML)

#apply the labels to map
m %>%addPolygons(fillColor = ~pal(median_hh_income),
              color = "white",
              weight = 0.5,
              opacity = 1,
              fillOpacity = 0.65,
              highlightOptions = highlightOptions(
                weight = 3,
                fillOpacity = 0.8,
                color = "red",
                bringToFront = TRUE),
              label = labels, #label addition to code
              labelOptions = labelOptions(
                style = list("font-weight" = "normal", padding = "3px 8px"),
                textsize = "15px",direction = "auto"))

Add Bold Data Labels

Add Legend

m %>%addPolygons(fillColor = ~pal(median_hh_income),
              color = "white",
              weight = 0.5,
              opacity = 1,
              fillOpacity = 0.65,
              label = labels,
              highlightOptions = highlightOptions(
                weight = 3,
                fillOpacity = 0.8,
                color = "red",
                bringToFront = TRUE),
              labelOptions = labelOptions(
                style = list("font-weight" = "normal", padding = "3px 8px"),
                textsize = "15px",
                direction = "auto")) %>%
  addLegend(pal = pal, values = ~median_hh_income, opacity = 0.7,
            title = "Median HH Income",position = "topright")

Add Legend

City of Surrey example